package com.wang.myapplication.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.wang.myapplication.R;
import com.wang.myapplication.base.BaseActivity;
import com.wang.myapplication.example.ExBasicActivity;
import com.wang.myapplication.example.ExDrawerActivity;
import com.wang.myapplication.example.ExEmptyActivity;
import com.wang.myapplication.example.ExFullscreenActivity;
import com.wang.myapplication.example.ExLoginActivity;
import com.wang.myapplication.example.ExMainActivity;
import com.wang.myapplication.example.ExScrollingActivity;
import com.wang.myapplication.example.ExSettingsActivity;
import com.wang.myapplication.fragment.BBSFragment;
import com.wang.myapplication.fragment.DocumentFragment;
import com.wang.myapplication.fragment.FirstFragment;
import com.wang.myapplication.fragment.KnowFragment;

import java.util.ArrayList;

import butterknife.BindView;

public class HomeActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    public static String PARAMKEY_NAME = "paramkey_name";
    public ViewPager viewPager;
    public ArrayList<Fragment> mFragmentArrayList;
    FragmentPagerAdapter mFragmentPagerAdapter;
    public BottomNavigationView navigation;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;
    @BindView(R.id.nav_view)
    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView listView;
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        //DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        //NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);




    }





    public static void startActSendParams(Activity pContext, String pName) {
        Intent intent = new Intent(pContext, HomeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(PARAMKEY_NAME, pName);
        intent.putExtras(bundle);
        pContext.startActivity(intent);

    }

    @Override
    public void initParams(Bundle parms) {
        //Toast.makeText(this, "我是取出来的参数" + parms.getString(PARAMKEY_NAME), Toast.LENGTH_SHORT).show();

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_home;
    }

    @Override
    public void initView(Bundle savedInstanceState, View view) {

    }


    @Override
    public void doBusiness(Context mContext) {
        new BaseAdapter() {
            @Override
            public int getCount() {
                return 0;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                return null;
            }
        };

        viewPager = findViewById(R.id.content_viewPager);
        mFragmentArrayList = new ArrayList<>();
        FirstFragment f1 = new FirstFragment();
        DocumentFragment f2 = new DocumentFragment();
        KnowFragment f3 = new KnowFragment();
        BBSFragment f4 = new BBSFragment();
        mFragmentArrayList.add(f1);
        mFragmentArrayList.add(f2);
        mFragmentArrayList.add(f3);
        mFragmentArrayList.add(f4);
        viewPager.setOffscreenPageLimit(mFragmentArrayList.size()-1);
        //FragmentUtils.add(getSupportFragmentManager(),mFragmentArrayList,,0);//add了可是就不能滑动了呀哎。
        mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentArrayList.size();
            }

        };

        viewPager.setAdapter(mFragmentPagerAdapter);


        //监听点击事件
        navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        viewPager.setCurrentItem(0);
                        //getSupportFragmentManager().beginTransaction().replace(R.id.flayoutContent, fragment1).commit();
                        return true;
                    case R.id.navigation_document:
                        viewPager.setCurrentItem(1);
                        //getSupportFragmentManager().beginTransaction().replace(R.id.flayoutContent, fragment2).commit();
                        return true;
                    case R.id.navigation_know:
                        viewPager.setCurrentItem(2);
                        //getSupportFragmentManager().beginTransaction().replace(R.id.flayoutContent, fragment1).commit();
                        return true;
                    case R.id.navigation_bbs:
                        viewPager.setCurrentItem(3);
                        //getSupportFragmentManager().beginTransaction().replace(R.id.flayoutContent, fragment2).commit();
                        return true;
                }
                return false;
            }
        });


        //viewpager监听滑动状态 设置bottom状态变化
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            MenuItem prevMenuItem = null;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                //设置上个未选择状态
                if (prevMenuItem != null) {
                    prevMenuItem.setChecked(false);
                } else {
                    navigation.getMenu().getItem(0).setChecked(false);
                }
                //设置当前是选择状态
                navigation.getMenu().getItem(position).setChecked(true);
                prevMenuItem = navigation.getMenu().getItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


    @Override
    public void widgetClick(View v) {

    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        // Toast.makeText(this, ""+id, Toast.LENGTH_SHORT).show();
        if (id == R.id.nav_collect_file) {
            //

        } else if (id == R.id.nav_my_downloader) {
            //
            startActivity(MyDownloadActivity.class);


        } else if (id == R.id.nav_collect_bbs) {

        } else if (id == R.id.nav_my_published) {

        } else if (id == R.id.nav_my_thumb) {

        } else if (id == R.id.nav_my_comment) {

        } else if (id == R.id.nav_my_rank) {

        } else if (id == R.id.nav_my_message) {

        } else if (id == R.id.nav_change_user) {

        } else if (id == R.id.nav_ExEmptyActivity) {
            startActivity(new Intent(this, ExEmptyActivity.class));
        } else if (id == R.id.nav_ExteBaseActivity) {
        } else if (id == R.id.nav_ExBasicActivity) {
            startActivity(new Intent(this, ExBasicActivity.class));
        } else if (id == R.id.nav_ExBottomNavigActivity) {
            startActivity(new Intent("my_action_exbottomnavig_activity"));

        } else if (id == R.id.nav_ExDrawerActivity) {
            startActivity(new Intent(this, ExDrawerActivity.class));

        } else if (id == R.id.nav_ExFullscreenActivity) {
            startActivity(new Intent(this, ExFullscreenActivity.class));

        } else if (id == R.id.nav_ExLoginActivity) {
            startActivity(new Intent(this, ExLoginActivity.class));

        } else if (id == R.id.nav_ExMainActivity) {
            startActivity(new Intent(this, ExMainActivity.class));

        } else if (id == R.id.nav_ExScrollingActivity) {
            startActivity(new Intent(this, ExScrollingActivity.class));

        } else if (id == R.id.nav_ExSettingsActivity) {
            startActivity(new Intent(this, ExSettingsActivity.class));

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }

    }


    //    @Override
    //    public boolean onCreateOptionsMenu(Menu menu) {
    //        // Inflate the menu; this adds items to the action bar if it is present.
    //        getMenuInflater().inflate(R.menu.drawer, menu);
    //        return true;
    //    }

    //    @Override
    //    public boolean onOptionsItemSelected(MenuItem item) {
    //        // Handle action bar item clicks here. The action bar will
    //        // automatically handle clicks on the Home/Up button, so long
    //        // as you specify a parent activity in AndroidManifest.xml.
    //        int id = item.getItemId();
    //
    //        //noinspection SimplifiableIfStatement
    //        if (id == R.id.action_settings) {
    //            return true;
    //        }
    //
    //        return super.onOptionsItemSelected(item);
    //    }

    //    @SuppressWarnings("StatementWithEmptyBody")
    //    @Override
    //    public boolean onNavigationItemSelected(MenuItem item) {
    //        // Handle navigation view item clicks here.
    //        int id = item.getItemId();
    //
    //        if (id == R.id.nav_camera) {
    //            // Handle the camera action
    //        } else if (id == R.id.nav_gallery) {
    //
    //        } else if (id == R.id.nav_slideshow) {
    //
    //        } else if (id == R.id.nav_manage) {
    //
    //        } else if (id == R.id.nav_share) {
    //
    //        } else if (id == R.id.nav_send) {
    //
    //        }
    //
    //        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    //        drawer.closeDrawer(GravityCompat.START);
    //        return true;
    //    }
}
